package com.heima.action;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.heima.pojo.BaseDict;
import com.heima.pojo.Customer;
import com.heima.pojo.QueryVo;
import com.heima.pojo.Result;
import com.heima.service.CustomerService;

import cn.itcast.utils.Page;

@Controller
@RequestMapping("/customer")
public class CustomerAction {
	@Resource
	CustomerService customerService;
	
	
	@RequestMapping("/list")
	public String list(Model model,QueryVo vo) throws UnsupportedEncodingException {
		//get乱码解决
		if (vo.getCustName() != null) {
			vo.setCustName(new String(vo.getCustName().getBytes("ISO8859-1"),"UTF-8"));
		}
		
		
		//获取客户来源where条件
		List<BaseDict> listCustomSource = customerService.findDictByTypeCode("002");
		model.addAttribute("fromType", listCustomSource);
		//获取客户所属行业where条件
		List<BaseDict> listIndustory = customerService.findDictByTypeCode("001");
		model.addAttribute("industryType", listIndustory);
		//获取客户级别where条件
		List<BaseDict> listLevel = customerService.findDictByTypeCode("006");
		model.addAttribute("levelType", listLevel);
		
		
		//设置参数回显
		model.addAttribute("vo", vo);
		
		
		//查询客户列表
		List<Customer> list = customerService.findCustomerList(vo);
		//查询客户符合条件的总记录数
		Integer customerCount = customerService.findCustomerCount(vo);
		
		//初始化分页对象page
		Page<Customer> pageObj=new Page<>();
		pageObj.setPage(vo.getPage());//设置当前查询的页
		pageObj.setRows(list);//设置当前页显示的数据
		pageObj.setSize(vo.getRows());//一页显示的条数
		pageObj.setTotal(customerCount);//一共多少条记录数
		model.addAttribute("page", pageObj);
		
		return "customer";
	}
	
	@RequestMapping("/delete")
	public @ResponseBody Result delete(Integer id,HttpServletResponse response) {
		Result result = customerService.deleteCustomerById(id);
		return result;
	}
	
	@RequestMapping("/edit")
	public @ResponseBody Customer findCustomerById(Integer id) {
		Customer customer = customerService.findCustomerById(id);
		return customer;
	}
	@RequestMapping("/update")
	public @ResponseBody Result updateCustomer(Customer customer) {
		Result result = customerService.updateCustomerById(customer);
		return result;
	}
	
}
